// pages/caseList/caseList.js
const app = getApp()

Page({
  data: {
    searchKeyword: '',
    filteredCases: [],
    allCases: [],
    categories: [],
    activeCategory: 0 // 添加当前激活的分类
  },
  
  onLoad: function() {
    // 获取所有案例和分类
    const allCases = app.globalData.cases;
    const categories = app.globalData.categories;
    
    this.setData({
      allCases: allCases,
      filteredCases: allCases,
      categories: categories
    });
  },
  
  // 搜索输入处理
  onSearchInput: function(e) {
    const keyword = e.detail.value.trim().toLowerCase();
    this.setData({ searchKeyword: keyword });
    
    if (!keyword) {
      this.setData({ filteredCases: this.data.allCases });
      return;
    }
    
    // 过滤案例
    const filtered = this.data.allCases.filter(item => {
      return item.title.toLowerCase().includes(keyword) || 
             item.subtitle.toLowerCase().includes(keyword);
    });
    
    this.setData({ filteredCases: filtered });
  },
  
  // 添加分类切换方法
  switchCategory: function(e) {
    const categoryId = parseInt(e.currentTarget.dataset.id);
    let filtered = [];
    
    if (categoryId === 0) {
      filtered = this.data.allCases;
    } else {
      filtered = this.data.allCases.filter(item => item.category === categoryId);
    }
    
    this.setData({
      activeCategory: categoryId,
      filteredCases: filtered
    });
  },
  
  // 获取分类名称
  getCategoryName: function(categoryId) {
    const category = this.data.categories.find(cat => cat.id === categoryId);
    return category ? category.name : '其他';
  }
});